/**
 * @(#)StarMatchService.java, 2013-2-14. Copyright 2013 RENREN, Inc. All rights
 *                            reserved. RENREN PROPRIETARY/CONFIDENTIAL. Use is
 *                            subject to license terms.
 */
package com.wennimei.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.wennimei.util.DataUtil;
import com.wennimei.util.FileProcessor;
import com.wennimei.util.FileUtil;

/**
 * @author shichen.feng@renren-inc.com
 */
@Service
public class StarMatchService {

    private static final Map<String, String> map = new HashMap<String, String>();

    @PostConstruct
    void init() {
        FileUtil.processFile(DataUtil.STAR_MATCH, new FileProcessor() {

            @Override
            public void process(String line) {
                String[] array = line.split("\\t");
                map.put(array[0], array[1]);
            }
        });
    }

    public String get(String content) {
        for (String s: map.keySet()) {
            String[] stars = s.split(",");
            if (content.matches("(.*" + stars[0] + ".*" + stars[1] + ".*|.*"
                    + stars[1] + ".*" + stars[0] + ".*)"))
                return stars[0] + "配" + stars[1] + ":" + map.get(s);
        }
        return "怎么说呢，很配就是啦！";
    }

    public static boolean isStarMatch(String content) {
        for (String s: map.keySet()) {
            String[] stars = s.split(",");
            if (content.matches("(.*" + stars[0] + ".*" + stars[1] + ".*|.*"
                    + stars[1] + ".*" + stars[0] + ".*)"))
                return true;
        }
        return false;
    }
}
